package com.gitee.mars.tourserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mars.tourserver.constant.Constant;
import com.gitee.mars.tourserver.entity.ScenicArea;
import com.gitee.mars.tourserver.manager.TourClawerManager;
import com.gitee.mars.tourserver.mapper.ScenicAreaMapper;
import com.gitee.mars.tourserver.request.ScenicAreaRequest;
import com.gitee.mars.tourserver.response.base.PageInfo;
import com.gitee.mars.tourserver.service.IScenicAreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Objects;


/**
 * ScenicArea 实现层
 *
 * @Author wq
 * @Date 2021-04-11
 **/
@Service
public class ScenicAreaServiceImpl extends ServiceImpl<ScenicAreaMapper, ScenicArea> implements IScenicAreaService {

    @Resource
    private TourClawerManager clawerManager;

    @Override
    public void acquire(String city) {
        try {
            List<ScenicArea> scenicAreas = clawerManager.acquireScenicArea(city);
            scenicAreas.forEach(this::save);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void acquireGlobalCity() {
        String[] citys = Constant.citys;
        for (int i = 0; i < citys.length; i++) {
            try {
                List<ScenicArea> scenicAreas = clawerManager.acquireScenicArea(citys[i]);
                scenicAreas.forEach(this::save);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo<ScenicArea> list(ScenicAreaRequest request) {
        IPage<ScenicArea> page = new Page<>(request.getPageNumber(), request.getPageSize());

        LambdaQueryWrapper<ScenicArea> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(request.getName())) {
            wrapper.like(ScenicArea::getName, request.getName());
        }
        if (StringUtils.isNotEmpty(request.getLocation())) {
            wrapper.like(ScenicArea::getLocation, request.getLocation());
        }
        if (Objects.nonNull(request.getLevel())) {
            if (request.getLevel() == 1) {
                wrapper.eq(ScenicArea::getLevel, "4A景区");
            } else {
                wrapper.eq(ScenicArea::getLevel, "5A景区");
            }
        }
        wrapper.orderByDesc(ScenicArea::getCreateTime);
        List<ScenicArea> list = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(list)) {
            return new PageInfo<>(page.getTotal(), list);
        }
        return null;
    }
}